package com.fkcai.hansoul.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fkcai.hansoul.model.entity.Post;
import com.fkcai.hansoul.model.vo.post.GetPostVo;
import com.fkcai.hansoul.model.vo.post.PostFullInfoVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用于操作帖子表
 */
@Mapper
public interface PostMapper extends BaseMapper<Post> {
	/**
	 * 根据用户 ID 查询用户发布的帖子
	 *
	 * @param userId 用户 ID
	 * @return 帖子列表含用户基础信息
	 */
	@Select("SELECT p.*, " +
			"u.user_id as user_userId, " +
			"u.avatar_url as user_avatarUrl, " +
			"u.username as user_username " +
			"FROM post p " +
			"LEFT JOIN user u ON p.author_id = u.user_id " +
			"WHERE p.author_id = #{userId} " +
			"ORDER BY p.create_time DESC")
	@Results({
			@Result(property = "postId", column = "post_id"),
			@Result(property = "title", column = "title"),
			@Result(property = "categoryId", column = "category_id"),
			@Result(property = "postStatus", column = "post_status"),
			@Result(property = "likeCount", column = "like_count"),
			// 映射嵌套的 user 对象
			@Result(property = "user.userId", column = "user_userId"),
			@Result(property = "user.avatarUrl", column = "user_avatarUrl"),
			@Result(property = "user.username", column = "user_username"),
			// 关联查询图片列表
			@Result(property = "imageUrls", column = "post_id",
					javaType = List.class,
					many = @Many(select = "com.fkcai.hansoul.mapper.PostImageMapper.selectImageUrlsByPostId"))
	})
	List<GetPostVo> getPostsByUserId (@Param("userId") Integer userId);
	
	/**
	 * 根据帖子 ID 查询帖子详情
	 *
	 * @param postId 帖子 ID
	 * @return 帖子详情
	 */
	@Select("SELECT p.*, " +
			"u.user_id as user_userId, " +
			"u.avatar_url as user_avatarUrl, " +
			"u.username as user_username " +
			"FROM post p " +
			"LEFT JOIN user u ON p.author_id = u.user_id " +
			"WHERE p.post_id = #{postId} " +
			"ORDER BY p.create_time DESC")
	@Results({
			@Result(property = "postId", column = "post_id"),
			@Result(property = "title", column = "title"),
			@Result(property = "content", column = "content"),
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "categoryId", column = "category_id"),
			@Result(property = "postStatus", column = "post_status"),
			@Result(property = "viewCount", column = "view_count"),
			@Result(property = "likeCount", column = "like_count"),
			@Result(property = "commentCount", column = "comment_count"),
			@Result(property = "shareCount", column = "share_count"),
			@Result(property = "location", column = "location"),
			@Result(property = "user.userId", column = "user_userId"),
			@Result(property = "user.avatarUrl", column = "user_avatarUrl"),
			@Result(property = "user.username", column = "user_username"),
			@Result(property = "imageUrls", column = "post_id",
					javaType = List.class,
					many = @Many(select = "com.fkcai.hansoul.mapper.PostImageMapper.selectImageUrlsByPostId"))
	})
	PostFullInfoVo getPostByPostId (@Param("postId") Integer postId);
	
	
	/**
	 * 分类获取所有公开的帖子
	 *
	 * @param offset   跳过的数量
	 * @param pageSize 每页数量
	 * @return 帖子列表
	 */
	@Select("SELECT p.*, " +
			"u.user_id as user_userId, " +
			"u.avatar_url as user_avatarUrl, " +
			"u.username as user_username " +
			"FROM post p " +
			"LEFT JOIN user u ON p.author_id = u.user_id " +
			"WHERE p.post_status = 'public' " +
			"ORDER BY RAND(#{randomSeed}) " + // 随机排序, 加上随机种子
			"LIMIT #{offset}, #{pageSize}")
	@Results({
			@Result(property = "postId", column = "post_id"),
			@Result(property = "title", column = "title"),
			@Result(property = "categoryId", column = "category_id"),
			@Result(property = "postStatus", column = "post_status"),
			@Result(property = "likeCount", column = "like_count"),
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "user.userId", column = "user_userId"),
			@Result(property = "user.avatarUrl", column = "user_avatarUrl"),
			@Result(property = "user.username", column = "user_username"),
			@Result(property = "imageUrls", column = "post_id",
					javaType = List.class,
					many = @Many(select = "com.fkcai.hansoul.mapper.PostImageMapper.selectImageUrlsByPostId"))
	})
	List<GetPostVo> selectAllPublicPosts (@Param("offset") int offset,
	                                      @Param("pageSize") int pageSize,
	                                      @Param("randomSeed") int randomSeed
	);
	
	
	/**
	 * 根据分类ID分页获取公开帖子
	 *
	 * @param categoryId 分类ID
	 * @param offset     偏移量
	 * @param pageSize   每页数量
	 */
	@Select("SELECT p.*, " +
			"u.user_id as user_userId, " +
			"u.avatar_url as user_avatarUrl, " +
			"u.username as user_username " +
			"FROM post p " +
			"LEFT JOIN user u ON p.author_id = u.user_id " +
			"WHERE p.category_id = #{categoryId} AND p.post_status = 'public' " +
			"ORDER BY RAND(#{randomSeed}) " +
			"LIMIT #{offset}, #{pageSize}")
	@Results({
			@Result(property = "postId", column = "post_id"),
			@Result(property = "title", column = "title"),
			@Result(property = "categoryId", column = "category_id"),
			@Result(property = "postStatus", column = "post_status"),
			@Result(property = "likeCount", column = "like_count"),
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "user.userId", column = "user_userId"),
			@Result(property = "user.avatarUrl", column = "user_avatarUrl"),
			@Result(property = "user.username", column = "user_username"),
			@Result(property = "imageUrls", column = "post_id",
					javaType = List.class,
					many = @Many(select = "com.fkcai.hansoul.mapper.PostImageMapper.selectImageUrlsByPostId"))
	})
	List<GetPostVo> selectPublicPostsByCategoryId (@Param("categoryId") Integer categoryId,
	                                               @Param("offset") int offset,
	                                               @Param("pageSize") int pageSize,
	                                               @Param("randomSeed") int randomSeed
	);
	
	
	/**
	 * 获取公开帖子总数
	 *
	 * @return 公开帖子总数
	 */
	@Select("SELECT COUNT(*) FROM post WHERE post_status = 'public'")
	Long countAllPublicPosts ();
	
	/**
	 * 根据分类 ID 获取公开帖子总数
	 *
	 * @param categoryId 分类 ID
	 * @return 分类后公开帖子总数
	 */
	@Select("SELECT COUNT(*) FROM post WHERE category_id = #{categoryId} AND post_status = 'public'")
	Long countPublicPostsByCategoryId (@Param("categoryId") Integer categoryId);
}
